/**
 * @{#}BeanToXml.java Created on Dec 15, 2015 3:08:07 PM
 * <p>
 * Copyright (c) Dec 15, 2015 by ZHONGBO software.
 */
package com.fingard.dsp.bank.directbank.vbao03.trade.utils;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;


/**
 * @author 作者 E-mail:lirp@chinavbao.com 
 * @version 1.0
 * 类说明 
 */
public class BeanToMapUtils {

    public static SortedMap<String, String> transBean2Map(Object obj, String handlerType, String InputCharset, boolean flag) {

        if (obj == null) {
            return null;
        }
        SortedMap<String, String> map = new TreeMap<String, String>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                // 过滤class属性  
                if (!key.equals("class")) {
                    // 得到property对应的getter方法  
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);
                    StringBuffer attribName = new StringBuffer();  //类中属性字段首字母是小写的
                    attribName.append(key.substring(0, 1).toUpperCase());
                    attribName.append(key.substring(1));
                    if (attribName.toString().equals("OrderInfos")) {
                        continue;
                    }
                    if (null != value) {
                        if (flag) {
                            map.put(attribName.toString(), URLEncodeParams(attribName.toString(), (String) value, InputCharset));
                        } else {
                            map.put(attribName.toString(), (String) value);
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("transBean2Map Error " + e);
        }

        return map;
    }

    public static Object encodeOrderInfos(Object obj, String handlerType, String InputCharset) {

        if (obj == null) {
            return null;
        }

        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                // 过滤class属性  
                if (!key.equals("class")) {
                    // 得到property对应的getter方法  
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);
                    StringBuffer attribName = new StringBuffer();  //类中属性字段首字母是小写的
                    attribName.append(key.substring(0, 1).toUpperCase());
                    attribName.append(key.substring(1));

                    if (checkURLDecoderParams(attribName.toString())) {
                        PropertyDescriptor pd = new PropertyDescriptor(key, obj.getClass());
                        Method wM = pd.getWriteMethod();//获得写方法
                        wM.invoke(obj, URLEncoder.encode((String) value, InputCharset));
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("encodeOrderInfos Error " + e);
        }
        return obj;
    }

    public static String URLEncodeParams(String key, String value, String InputCharset) {
        return value;
    }

    public static boolean checkURLDecoderParams(String key) {
        return false;
    }

    public static SortedMap<String, String> transBean2OriMap(Object obj, String handlerType) {

        if (obj == null) {
            return null;
        }
        SortedMap<String, String> map = new TreeMap<String, String>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                // 过滤class属性  
                if (!key.equals("class")) {
                    // 得到property对应的getter方法  
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);
                    StringBuffer attribName = new StringBuffer();  //类中属性字段首字母是小写的
                    attribName.append(key.substring(0, 1).toUpperCase());
                    attribName.append(key.substring(1));
                    if (attribName.toString().equals("OrderInfos")) {
                        continue;
                    }
                    if (null != value) {
                        map.put(attribName.toString(), (String) value);
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("transBean2Map Error " + e);
        }

        return map;
    }

    public static String URLDecoderParams(String key, String value, String InputCharset) {
        return value;
    }


    /**
     * 将一个 Map 对象转化为一个 JavaBean
     * @param type 要转化的类型
     * @param map 包含属性值的 map
     * @return 转化出来的 JavaBean 对象
     * @throws IntrospectionException
     *             如果分析类属性失败
     * @throws IllegalAccessException
     *             如果实例化 JavaBean 失败
     * @throws InstantiationException
     *             如果实例化 JavaBean 失败
     * @throws InvocationTargetException
     *             如果调用属性的 setter 方法失败
     */
    public static Object convertMap(Class type, Map map)
            throws IntrospectionException, IllegalAccessException,
            InstantiationException, InvocationTargetException {
        BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
        Object obj = type.newInstance(); // 创建 JavaBean 对象

        // 给 JavaBean 对象的属性赋值
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();

            if (map.containsKey(propertyName)) {
                // 下面一句可以 try 起来，这样当一个属性赋值失败的时候就不会影响其他属性赋值。
                Object value = map.get(propertyName);

                Object[] args = new Object[1];
                args[0] = value;

                descriptor.getWriteMethod().invoke(obj, args);
            }
        }
        return obj;
    }
}
